package com.yiwei.zxp.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

/**
 * Created by xsooy-pc on 18-5-11.
 */

public abstract class OnLoadMoreHomeListener extends RecyclerView.OnScrollListener {

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of home to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.


    private int currentPage = 1;

//    public boolean enable = true;


//    @Override
//    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//        super.onScrollStateChanged(recyclerView, newState);
//        if(!loading && !recyclerView.canScrollVertically(1)){
//            loading = true;
//            loadMoreDate();
//        }
//    }

    public static boolean isVisBottom(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //屏幕中最后一个可见子项的position
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        //当前屏幕所看到的子项个数
        int visibleItemCount = layoutManager.getChildCount();
        //当前RecyclerView的所有子项个数
        int totalItemCount = layoutManager.getItemCount();
        //RecyclerView的滑动状态
        int state = recyclerView.getScrollState();
        if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && state == recyclerView.SCROLL_STATE_IDLE) {
            return true;
        } else {
            return false;
        }
    }

    protected boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
//        if (!enable) {
//            return;
//        }

//        LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
////        findFirstVisibleItemPosition()
////        findFirstCompletelyVisibleItemPosition()
////        findLastVisibleItemPosition()
////        findLastCompletelyVisibleItemPosition()
//
////          //当前屏幕所看到的子项个数
//        int visibleItemCount = recyclerView.getChildCount();
//        //当前RecyclerView的所有子项个数
//        int totalItemCount = llm.getItemCount();
//        //屏幕中第一个可见子项的position
//        int firstVisibleItem = llm.findFirstVisibleItemPosition();
//        if (loading) {
//            if (totalItemCount > previousTotal) {
//                loading = false;
//                previousTotal = totalItemCount;
//            }
//        }
//        if ((!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))||isSlideToBottom(recyclerView)) {
////        if (!loading && (visibleItemCount + firstVisibleItem) >= totalItemCount){
//            currentPage++;
//
//            onLoadMore(currentPage);
//
//            loading = true;
//        }
        Log.i("dasdassdasddasdas", isSlideToBottom(recyclerView) + "到底啦？");
        if(isSlideToBottom(recyclerView)){
            currentPage++;

            onLoadMore(currentPage);
        }

    }

    public void setPage(int page) {
        currentPage = page;
        this.previousTotal = 0;
        this.loading = true;
//        enable = true;
    }

    public abstract void onLoadMore(int page);

//    public abstract void onBottom();
}
